<template>
  <div class="el-dialog-div">
    <el-dialog
      ref="internetmgr"
      width="30%"
      title="选择机器"
      :visible.sync="params.visible"
      class="el-machine"
    >
      <compTree
        :tree-show-checkbox="true"
        :comp-tree-height="params.treeHeight||350"
        :tree-data="treeData"
        :highlight-current="true"
        :show-search="false"
        :tree-default-checked-keys="treeDefaultCheckedKeys"
        @treeNodeCheck="treeNodeCheck"
      />
      <span
        slot="footer"
        class="dialog-footer"
      >
        <dialog-btn />

      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getAgentList } from '@/api/core'
import Global from '@/../public/js/global'
import { ParamArr, SQLTable } from '@/../public/js/paramarr'
export default {
  name: 'SelectRoom',
  props: {
    // eslint-disable-next-line vue/require-default-prop
    params: {
      type: Object,
      default() {
        return {
          visible: false,
          ids: '' // 选择数据
        }
      }
    }
  },
  data() {
    return {
      treeData: [],
      treeDefaultCheckedKeys: [],
      resule: ''
    }
  },
  created() {
    getAgentList(new ParamArr()).then(vPar => {
      this.lpfInitWindow(vPar)
    })
  },
  methods: {
    treeNodeCheck(a, b) {
      this.resule = this.getRsturn(b.checkedNodes)
    },
    getRsturn(nodeCheck) {
      var arrID = []
      var arrIP = []
      var arrName = []
      for (var i = 0; i < nodeCheck.length; i++) {
        var node = nodeCheck[i]
        switch (node.type) {
          case 2:
            if (node.id !== '') {
              arrID.push(node.id)
              arrIP.push(node.ip)
              arrName.push(node.name)
            }
            break
        }
      }
      this.arrID = arrID
      var obj = {}
      obj.arrID = arrID
      obj.arrIP = arrIP
      obj.arrName = arrName
      return obj
    },
    lpfInitWindow(vPar) {
      var nodes = []
      var nodeMap = new Map()
      var checked = []
      // 分组
      var table = new SQLTable(vPar.getTable('k_agent_group'))
      table.first()
      while (!table.eof()) {
        var pnode = Global.getTreeviewPNode(nodeMap, nodes, table.getValueByName('PID'))
        var node = {}
        // data
        node['id'] = table.getValueByName('ID')
        node['text'] = table.getValueByName('Name')
        node['type'] = 1/* cmtGroup*/
        node.folder = 1
        // add
        pnode.push(node)
        nodeMap.set(table.getValueByName('ID'), node)
        // next
        table.next()
      }
      // 机器
      var tabler = new SQLTable(vPar.getTable('k_agent'))
      tabler.first()
      while (!tabler.eof()) {
        var pnoder = Global.getTreeviewPNode(nodeMap, nodes, tabler.getValueByName('PID'))
        var nodeR = {}
        // data
        nodeR['id'] = tabler.getValueByName('ID')
        nodeR['ip'] = tabler.getValueByName('IP')
        nodeR['name'] = tabler.getValueByName('Name')
        nodeR['icon'] = Global.getSystemIconStr(tabler.getValueByName('System'))
        nodeR['text'] = tabler.getValueByName('IP') + '[' + tabler.getValueByName('Name') + ']'
        nodeR['type'] = 2/* cmtComputer*/
        if (this.params.ids.indexOf(nodeR['id']) > -1) {
          this.treeDefaultCheckedKeys.push(nodeR.id)
          checked.push(nodeR)
        }

        // add
        pnoder.push(nodeR)
        // next
        tabler.next()
      }
      this.resule = this.getRsturn(checked)

      this.$closeLoading()
      this.treeData = nodes
    },
    confirm() {
      if (this.$route.path === '/system/menu1/internetmgr') {
        if (this.arrID.length === 0) {
          this.$message.warning('请选择机器。')
          return
        }
      }
      // 如果当前传入回调则使用回调
      if (this.params.confirm !== undefined) {
        this.params.confirm(this.resule)
      } else {
        this.$emit('confirm', this.resule)
      }
    }
  }
}
</script>
<style lang="scss" scoped>

</style>

